package com.example.visitor.controller;


import com.example.visitor.converter.VisitorConverter;
import com.example.visitor.mapper.*;
import com.example.visitor.pojo.Inter;
import com.example.visitor.pojo.Visitor;
import com.example.visitor.vo.PageVO;
import com.example.visitor.vo.VisitorVO;
import com.example.visitor.mapper.VisitorMapper;
import com.example.visitor.exception.ServiceException;
//import com.example.visitor.redis.RedisUtil;
//import com.example.visitor.service.VisitorService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.MDC;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.common.base.select.SelectByPrimaryKeyMapper;
import tk.mybatis.mapper.entity.Example;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * @author  SongPeng
 * @date  2021/11/11 20:38
 * @version 1.0
 * 访客管理模块
 */


@RestController
@RequestMapping("/visitor")
public class VisitorController {
    @ModelAttribute
    public void doString(){
        MDC.put("operateModuleName","访客管理模块");
    }
//    @Autowired
//    private VisitorService visitorService;
    @Autowired
    private VisitorMapper visitorMapper;
    @Autowired
    private InterMapper interMapper;
    @Autowired
    private VisitorConverter visitorConverter;
//    @Autowired
//    RedisUtil redisUtil;
//    public String report_key = "report";

    private static final Logger logger = LogManager.getLogger(VisitorController.class);
    /**
     * 添加访客记录
     * @param visitorVO
     * @return
     */
    @PostMapping("/regist")
    public void add(@RequestBody @Validated VisitorVO visitorVO) {
        logger.info("添加预约访客记录");
        @NotBlank(message = "访客名不能为空") String visitorName = visitorVO.getVisitorName();
        @NotNull(message = "被访人id不能为空") Long InterId = visitorVO.getInterId();
//        SelectByPrimaryKeyMapper<Object> interMapper;
        Inter inter = interMapper.selectByPrimaryKey(InterId);
        if (inter == null) {
            throw new ServiceException("请确定要访问的人员信息是否存在");
        }
        Visitor visitor = new Visitor();
        BeanUtils.copyProperties(visitorVO, visitor);
        visitor.setEtime(new Date());
        visitorMapper.insert(visitor);
    }
    /**
     *
     * 查询访客记录列表
     */
    @GetMapping("/listData")
    public PageVO<VisitorVO>  listData(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, VisitorVO visitorVO, @RequestParam  Date startTime, @RequestParam  Date endTime){
        PageHelper.startPage(pageNum, pageSize);
        Example o = new Example(Visitor.class);
        String visitorName = visitorVO.getVisitorName();
        Integer sex = visitorVO.getVisitorSex();
        String nation = visitorVO.getVisitorNation();
        String Address = visitorVO.getVisitorAddress();
        String visitorPhone = visitorVO.getVisitorPhone();
        Example.Criteria criteria = o.createCriteria();
        //降序排序
        o.setOrderByClause("etime desc");
        if (visitorName != null && !"".equals(visitorName)) {
            criteria.andLike("visitorName", "%" + visitorName + "%");
        }
        if (sex != null) {
            criteria.andEqualTo("visitorSex", sex);
        }
        if (nation != null && !"".equals(nation)) {
            criteria.andEqualTo("visitorNation", nation);
        }
        if (Address != null && !"".equals(Address)) {
            criteria.andEqualTo("visitorAddress", "%" + Address + "%");
        }
        if (visitorPhone != null) {
            criteria.andEqualTo("visitorPhone", visitorPhone);
        }
        if(startTime!=null&&endTime!=null){
            criteria.andBetween("etime", startTime, endTime);
        }
        List<Visitor> visitorList = visitorMapper.selectByExample(o);
        PageInfo<Visitor> info = new PageInfo<>(visitorList);
        List<VisitorVO> visitorVOS = visitorConverter.converterToVisitorVOList(visitorList);
        return new PageVO<>(info.getTotal(), visitorVOS);
    }
    /**
     * 访客登记报表
     * @return
     */
    @PostMapping("/VisitorReport")
    public List<Map<String, Object>> loginReport(){
        return visitorMapper.LeaveReport();
    }
    /**
     * 设置访客离开
     * @param visitorId
     * @return
     */
    @PutMapping("/set/{visitorId}")
    public void edit(@PathVariable Long visitorId) throws InterruptedException {
        logger.info("设置访客离开");
        //此时登记报表数据可能更新，保证一致性(延时双删
        Visitor visitor = visitorMapper.selectByPrimaryKey(visitorId);
        visitor.setLtime(new Date());
        visitorMapper.updateByPrimaryKeySelective(visitor);
    }
    /**
     * 删除访客
     * */
    @DeleteMapping("/delete/{visitorId}")
     public void delete(@PathVariable Long visitorId) throws InterruptedException {
        logger.info("删除访客");
        visitorMapper.deleteByPrimaryKey(visitorId);
    }
    /**
     * 访客详细
     * */
    @GetMapping("/detail/{visitorId}")
    public VisitorVO detail(@PathVariable Long visitorId){
        logger.info("访客详细");
        Visitor visitor = visitorMapper.selectByPrimaryKey(visitorId);
        VisitorVO visitorVO = visitorConverter.converterToVisitorVO(visitor);
        return visitorVO;
    }


}